/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientsDialog.java
 *
 * Created on 21 août 2011, 16:43:23
 */
package app.views;

import app.App;
import app.events.ClientSelectedListener;
import app.models.Client;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

/**
 *
 * @author Ghost
 */
public class ClientsDialog extends javax.swing.JDialog implements ClientSelectedListener {
    
    private ClientsGui clientsGui;
    private Client selectedClient = null;
    private boolean dialogOkButton = false;

    /** Creates new form ClientsDialog */
    public ClientsDialog() {
        super(App.getInstance().getMainFrame(), true);
        initComponents();
        
        clientsGui = new ClientsGui();
        clientsGui.setClients(App.getInstance().getRegistre().getClients());
        
        if (clientsGui == null) {
            JOptionPane.showMessageDialog(this, "Impossible de récuperer les "
                    + "clients.\n"
                    + "Veuillez redemarrer l'application!",
                    "Erreur!",
                    JOptionPane.ERROR_MESSAGE);
            this.dispose();
        }
        clientsGui.addClientSelectedListener(this);
        getContentPane().add(clientsGui, java.awt.BorderLayout.CENTER);
        
        this.pack();
        center();
    }
    
    private void center() {
        //Centering
        Toolkit kit = getToolkit();
        int x, y, width, height;
        Dimension screenSize = kit.getScreenSize();
        Insets insets = kit.getScreenInsets(getGraphicsConfiguration());
        width = getWidth();
        height = getHeight();
        
        x = (screenSize.width - insets.left - insets.right - width) / 2;
        y = (screenSize.height - insets.top - insets.bottom - height) / 2;
        
        this.setBounds(x, y, width, height);
    }
    
    public boolean showDialog() {
        this.setVisible(true);
        return dialogOkButton;
    }
    
    public Client getSelectedClient() {
        return selectedClient;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        actionPan = new javax.swing.JPanel();
        btValider = new javax.swing.JButton();
        btAnnuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Choisir un fidèle!");
        setAlwaysOnTop(true);
        setLocationByPlatform(true);
        setModal(true);

        btValider.setText("Valider");
        btValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btValiderActionPerformed(evt);
            }
        });

        btAnnuler.setText("Annuler");
        btAnnuler.setBorderPainted(false);
        btAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAnnulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout actionPanLayout = new javax.swing.GroupLayout(actionPan);
        actionPan.setLayout(actionPanLayout);
        actionPanLayout.setHorizontalGroup(
            actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, actionPanLayout.createSequentialGroup()
                .addContainerGap(248, Short.MAX_VALUE)
                .addComponent(btAnnuler)
                .addGap(18, 18, 18)
                .addComponent(btValider)
                .addContainerGap())
        );
        actionPanLayout.setVerticalGroup(
            actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, actionPanLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btValider)
                    .addComponent(btAnnuler))
                .addContainerGap())
        );

        getContentPane().add(actionPan, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btValiderActionPerformed
    Client cli = clientsGui.getSelectedClient();
    dialogOkButton = true;
    this.selectedClient = cli;
    dialogOkButton = true;
    this.dispose();
}//GEN-LAST:event_btValiderActionPerformed
    
private void btAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnnulerActionPerformed
    dialogOkButton = false;
    this.selectedClient = null;
    this.dispose();
}//GEN-LAST:event_btAnnulerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionPan;
    private javax.swing.JButton btAnnuler;
    private javax.swing.JButton btValider;
    // End of variables declaration//GEN-END:variables

    // -- EVENTS ------------------------------------------------------------ //
    @Override
    public void clientSelected(Client cli) {
        this.selectedClient = cli;
        dialogOkButton = true;
        this.dispose();
    }
}
